package com.handinglian.common.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

@Slf4j
public class CustomLogoutFilter extends LogoutFilter {

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        String pushId = request.getParameter("pushId");
        Subject subject = getSubject(request, response);
        Principal principal = (Principal) subject.getPrincipal();
        String redirectUrl = getRedirectUrl(request, response, subject);
        redirectUrl = redirectUrl+"?jobNum="+principal.getJobNum()+"&pushId="+pushId;
        try {
            subject.logout();
        } catch (SessionException ise) {
            log.debug("Encountered session exception during logout.  This can generally safely be ignored.", ise);
        }
        issueRedirect(request, response, redirectUrl);

        return false;
    }
}
